﻿namespace Edge.Utilities
{
    using System.Collections;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using DexterLib;

    /// <summary>
    /// Contains various video-related utilities.
    /// </summary>
    public class VideoUtilities
    {
        /// <summary>
        /// Gets an array of bitmaps which have been extracted from a movie file.
        /// </summary>
        /// <param name="moviePath">Path to the movie</param>
        /// <param name="frameCount">Number of frames to extract from the movie</param>
        /// <returns>An ArrayList of bitmaps</returns>
        public static ArrayList ExtractFrames(string moviePath, int frameCount)
        {
            ArrayList results = new ArrayList();

            string tempDir = Path.GetTempPath();

            Directory.CreateDirectory(tempDir);

            MediaDetClass md = new MediaDetClass();
            md.Filename = moviePath;
            md.CurrentStream = 0;

            int len = (int)md.StreamLength;

            int interval = len / frameCount;

            int imageNumber = 1;

            for (int i = 0; i < len && (imageNumber - 1) < frameCount; i = i + interval)
            {
                string bitmapName = tempDir + imageNumber.ToString() + ".bmp";
                string jpegName = tempDir + imageNumber.ToString() + ".jpg";

                md.WriteBitmapBits(i, 320, 240, bitmapName);

                Image img = Image.FromFile(bitmapName);
                img.Save(jpegName, ImageFormat.Jpeg);
                img.Dispose();

                File.Delete(bitmapName);

                imageNumber++;

                byte[] imageBytes = File.ReadAllBytes(jpegName);

                results.Add(imageBytes);
            }

            return results;
        }

        /// <summary>
        /// Formats the duration of a movie to a reader-friendly string.
        /// </summary>
        /// <param name="duration">Duration of the movie.</param>
        /// <returns>Formatted string.</returns>
        public static string FormatFileDuration(long duration)
        {
            long hours = duration / 3600;

            string hourString = string.Empty;

            if (hours > 0)
            {
                hourString = hours.ToString() + ":";
            }

            long remainingSeconds = duration % 3600;

            long minutes = remainingSeconds / 60;

            string minString = string.Empty;

            if (minutes < 10 && hours > 0)
            {
                minString = "0" + minutes.ToString() + ":";
            }
            else
            {
                minString = minutes.ToString() + ":";
            }

            remainingSeconds = remainingSeconds % 60;

            string secString = string.Empty;

            if (remainingSeconds < 10)
            {
                secString = "0" + remainingSeconds.ToString();
            }
            else
            {
                secString = remainingSeconds.ToString();
            }

            return hourString + minString + secString;
        }
    }
}
